<template>
  <div>
    <!-- "面包屑" -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">购物车</a></el-breadcrumb-item>
    </el-breadcrumb>
    
    <!-- 搜索框 -->
    <div style="padding: 10px 0; margin-top: 10px">
      <el-input 
        style="width: 125px; margin-left: 5px" 
        placeholder="请输入菜品名字" 
        v-model="name" 
        clearable
      ></el-input>
      <el-button style="margin-left: 5px" @click="load" circle>
        <el-icon><Search /></el-icon>
      </el-button>
      <el-button 
        style="margin-left: 20px" 
        type="warning" 
        @click="reset" 
        plain
      >
        <el-icon><Refresh /></el-icon>
        <span>重置</span>
      </el-button>
    </div>

    <!-- 右侧主体内容 -->
    <el-card style="margin-top: 10px">
      <el-table :data="tableData">
        <el-table-column prop="dish" label="菜品名称" width="200"></el-table-column>
        <el-table-column prop="price" label="菜品单价" width="400"></el-table-column>
        <el-table-column prop="count" label="点餐数量">
          <template #default="scope">
            <el-input-number v-model="scope.row.count" :min="0" :max="10" @change="changeCount(scope.row)"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="warning" style="min-height: 20px; min-width: 20px; text-align: center" @click="handle_remove(scope.row)" circle>
              <el-icon><Minus /></el-icon>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <div style="padding: 10px 0; text-align: center; margin-top: 20px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>

    <div style="margin: 10px 0; text-align: center">
      <div style="padding: 10px 0">当前已点菜品总价：<span style="color: orangered">¥ {{totalPrice}}</span></div>
      <el-button type="success" size="medium" @click="pay()" round>
        <el-icon><Coin /></el-icon>
        <span>支 付</span>
      </el-button>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import { Search, Refresh, Minus, Coin } from '@element-plus/icons-vue';

export default {
  name: "Dept",
  components: {
    Search,
    Refresh,
    Minus,
    Coin
  },
  data() {
    return {
      tableData: [],
      tempData: "",
      total: 0,
      pageNum: 1,
      pageSize: 5,
      form: {},
      dish: "",
      totalPrice: 0,
      username: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      tableid: localStorage.getItem("tableid") ? JSON.parse(localStorage.getItem("tableid")) : {},
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      // 请求分页相关数据
      request.get("/client/shopping/select", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          username: this.username,
          dish: this.dish
        }
      }).then(res => {
        console.log(res.content);
        this.tableData = res.content.data;
        this.totalPrice = res.content.totalprice;
        this.total = res.content.total;
      })
    },
    reset() {
      this.dish = "";
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    changeCount(row) {
      console.log(row);
      this.form = row;
      request.post("/client/shopping/change", this.form).then(res => {
        console.log(res.message);
        this.load();
      })
    },
    pay() {
      this.tableData.forEach(item => {
        this.tempData += item.dish;
        this.tempData += "*";
        this.tempData += item.count;
        this.tempData += ";";
      })
      request.get("/client/shopping/pay", {
        params: {
          tableid: this.tableid,
          username: this.username,
          detail: this.tempData,
          totalPrice: this.totalPrice
        }
      }).then(res => {
        console.log("res", res);
        if (res.success) {
          this.$message.success(res.message);
          this.load();
        } else {
          this.$message.error(res.message);
        }
      })
      this.tempData = "";
    },
    handle_remove(row) {
      this.form = row;
      this.$confirm('此操作将移除该菜品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handle_remove_confirm();
        this.load();
        this.$message.success("移除成功！");
      }).catch(() => {
        this.$message.info("已取消操作。");
      })
    },
    handle_remove_confirm() {
      console.log(this.form);
      request.post("/client/shopping/remove", this.form);
    }
  }
}
</script>

<style scoped>

</style>